package com.tom.jmgd.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tom.jmgd.common.DzApplicationCommon;
import com.tom.jmgd.entity.param.dz.*;
import com.tom.jmgd.service.DzService;
import com.tom.jmgd.util.HttpPostRequestUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DzServiceImpl implements DzService {
//    @Autowired
//    TCjLxWareMapper tCjLxWareMapper;

    //查询价格
    @Override
    public String queryPrice(String ... goodsids) throws Exception {
        //String[] goodsids = {"H022911N","B404811N"};
        QueryPriceDTO queryPriceDTO = createQueryPriceDTO(goodsids);
        String content = JSONObject.toJSONString(queryPriceDTO);
        BaseDTO baseDTO = BaseDTO.createBaseDTO(content, DzApplicationCommon.M_SEARCH_PRICE);
        String respJson = HttpPostRequestUtil.sendPostRequest(DzApplicationCommon.URL, JSONObject.toJSONString(baseDTO));
        System.out.println("响应信息：" + respJson);
        BaseResponse baseResponse = JSONObject.parseObject(respJson, BaseResponse.class);
        if (200 == baseResponse.getCode()) {
            List<QueryPriceResponse> queryPriceResponses = JSONObject.parseObject(baseResponse.getData(),
                    new TypeReference<List<QueryPriceResponse>>(){});
        }
        return respJson;
    }

    @Override
    public String queryPriceBatch() throws Exception {
        //int i = tCjLxWareMapper.selectAll1();
        return null;
    }

    public QueryPriceDTO createQueryPriceDTO(String ... goodsids) {
        QueryPriceDTO queryPriceDTO = new QueryPriceDTO();

        /**
         * 由B2B提供
         */
        queryPriceDTO.setSupplierid(DzApplicationCommon.SUPPLIERID);

        /**
         * 商品编码集合
         */
        queryPriceDTO.setGoodsid(Arrays.asList(goodsids));

        return queryPriceDTO;
    }

    //查询库存
    @Override
    public String queryStock(String ... goodsids) throws Exception {
        QueryStockDTO queryStockDTO = createQueryStockDTO(goodsids);
        String content = JSONObject.toJSONString(queryStockDTO);
        BaseDTO baseDTO = BaseDTO.createBaseDTO(content, DzApplicationCommon.M_SEARCH_STOCK);
        String respJson = HttpPostRequestUtil.sendPostRequest(DzApplicationCommon.URL, JSONObject.toJSONString(baseDTO));
        System.out.println("响应信息：" + respJson);
        BaseResponse baseResponse = JSONObject.parseObject(respJson, BaseResponse.class);
        if (200 == baseResponse.getCode()) {
            List<QueryStockResponse> queryStockResponses = JSONObject.parseObject(baseResponse.getData(),
                    new TypeReference<List<QueryStockResponse>>(){});
            System.out.println(JSONObject.toJSONString(queryStockResponses));
        }
        return respJson;
    }

    @Override
    public String queryStockBatch() throws Exception {
        return null;
    }


    public QueryStockDTO createQueryStockDTO(String ... goodsids) {
        QueryStockDTO queryStockDTO = new QueryStockDTO();

        /**
         * 由B2B提供
         */
        queryStockDTO.setSupplierid(DzApplicationCommon.SUPPLIERID);

        /**
         * 商品编码集合
         */
        queryStockDTO.setGoodsid(Arrays.asList(goodsids));

        return queryStockDTO;
    }


    @Override
    public String createOrder(String paymethod,String remark,int qty,BigDecimal price,String ... goodsids) throws Exception {
        PlaceAnOrderDTO placeAnOrderDTO = createPlaceAnOrderDTO(paymethod,remark,qty,price,goodsids);
        String content = JSONObject.toJSONString(placeAnOrderDTO);
        BaseDTO baseDTO = BaseDTO.createBaseDTO(content, DzApplicationCommon.M_CREATE_ORDER);
        String respJson = HttpPostRequestUtil.sendPostRequest(DzApplicationCommon.URL, JSONObject.toJSONString(baseDTO));
        System.out.println("响应信息：" + respJson);
        BaseResponse baseResponse = JSONObject.parseObject(respJson, BaseResponse.class);
        if (200 == baseResponse.getCode()) {
            PlaceAnOrderResponse placeAnOrderResponse = JSONObject.parseObject(baseResponse.getData(), PlaceAnOrderResponse.class);
            System.out.println(JSONObject.toJSONString(placeAnOrderResponse));
        }
        return respJson;
    }

    public PlaceAnOrderDTO createPlaceAnOrderDTO(String paymethod,String remark,int qty,BigDecimal price,String ... goodsids) {
        PlaceAnOrderDTO placeAnOrderDTO = new PlaceAnOrderDTO();
        /**
         * 由B2B提供
         */
        placeAnOrderDTO.setAddressid(DzApplicationCommon.ADDRESSID);

        /**
         * 由B2B提供
         */
        placeAnOrderDTO.setSupplierid(DzApplicationCommon.SUPPLIERID);

        /**
         * 接入方自定义订单编码，需要替换成接入方实际的订单编号
         */
        String orderCode = String.valueOf(System.currentTimeMillis());

        placeAnOrderDTO.setOrdercode(orderCode);

        System.out.println("订单编号"+orderCode);

        /**
         * 运费
         */
        placeAnOrderDTO.setFreight(new BigDecimal("0"));

        /**
         * 支付方式，按需填写
         */
        placeAnOrderDTO.setPaymethod(paymethod);

        /**
         * 备注
         */
        placeAnOrderDTO.setRemark(remark);

        //构建下单细项
        int itemcode = 0;
        List<PlaceAnOrderEntryDTO> items = new ArrayList<PlaceAnOrderEntryDTO>(goodsids.length);
        for (String goodsid : goodsids) {
            PlaceAnOrderEntryDTO placeAnOrderEntryDTO = new PlaceAnOrderEntryDTO();
            /**
             * 订单项编码，区分不同订单项，同一订单细项不可重复，可用1、2、3……简单区分
             */
            placeAnOrderEntryDTO.setItemcode(String.valueOf(itemcode++));

            /**
             * 商品编码
             */
            placeAnOrderEntryDTO.setGoodsid(goodsid);

            /**
             * 下单数量
             */
            placeAnOrderEntryDTO.setGoodsqty(qty);

            /**
             * 期望价格,传商品的期望价格
             */
            placeAnOrderEntryDTO.setExceptprice(price);

            items.add(placeAnOrderEntryDTO);
        }
        placeAnOrderDTO.setItems(items);
        return placeAnOrderDTO;
    }

    @Override
    public String queryOrderStatus(String orderCode) throws Exception {
        QueryOrderStatusDTO queryOrderStatusDTO = createQueryOrderStatusDTO(orderCode);
        String content = JSONObject.toJSONString(queryOrderStatusDTO);
        BaseDTO baseDTO = BaseDTO.createBaseDTO(content, DzApplicationCommon.M_SEARCH_ORDER);
        String respJson = HttpPostRequestUtil.sendPostRequest(DzApplicationCommon.URL, JSONObject.toJSONString(baseDTO));
        System.out.println("响应信息：" + respJson);
        BaseResponse baseResponse = JSONObject.parseObject(respJson, BaseResponse.class);
        if (200 == baseResponse.getCode()) {
            QueryOrderStatusResponse queryOrderStatusResponse = JSONObject.parseObject(baseResponse.getData(), QueryOrderStatusResponse.class);
            System.out.println(JSONObject.toJSONString(queryOrderStatusResponse));
        }
        return respJson;
    }

    public QueryOrderStatusDTO createQueryOrderStatusDTO(String ordercode) {
        QueryOrderStatusDTO queryOrderStatusDTO = new QueryOrderStatusDTO();
        /**
         * B2B或接入方订单编号
         */
        queryOrderStatusDTO.setB2bordercode(ordercode);
        return queryOrderStatusDTO;
    }


}
